/**
 * Title: ScrollBannerAdapter.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2013-12-16 下午1:41:05
 * Version 1.0
 */
package com.luoxudong.phonenumber.ui.widgets.scrollbanner;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.luoxudong.phonenumber.R;
import com.luoxudong.phonenumber.ui.adapters.AbsBaseAdapter;

/** 
 * ClassName: ScrollBannerAdapter
 * Description:TODO(这里用一句话描述这个类的作用)
 * Create by 罗旭东
 * Date 2013-12-16 下午1:41:05
 */
public class ScrollBannerAdapter extends AbsBaseAdapter<BannerItem>{
	private LayoutInflater mInflater;
	private Bitmap bitmap = null;
	
	public ScrollBannerAdapter(Context context, List<BannerItem> list) {
		super(context, list);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}
	
	@Override
	protected View getItemView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		
		if (convertView == null)
		{
			convertView = inflater.inflate(R.layout.scrollbanner_item_layout, null);
			
			viewHolder = new ViewHolder();
			viewHolder.scrollBarImageView = (ImageView)convertView.findViewById(R.id.imgView);
			convertView.setTag(viewHolder);
		}
		else 
		{
			viewHolder = (ViewHolder)convertView.getTag();
		}
		
		int size = getList() == null ? 0 : getList().size();
		
		if (size == 0)
		{
			return convertView;
		}
		
		BannerItem bannerItem = getItem(position % size);
		
		if (bannerItem != null)
		{
			if (bitmap != null)
			{
				bitmap.recycle();
			}
			
			if (bannerItem.getResId() != -1)
			{
				viewHolder.scrollBarImageView.setBackgroundResource(bannerItem.getResId());
			}
			else
			{
				bitmap = BitmapFactory.decodeFile(bannerItem.getLocalFilePath());
				viewHolder.scrollBarImageView.setBackground(new BitmapDrawable(context.getResources(), bitmap));
			}
			
		}
		return convertView;
	}
	
	public void releaseCache()
	{
		if (bitmap != null){
			bitmap.recycle();
		}
	}
	class ViewHolder
	{
		public ImageView scrollBarImageView = null;
	}

}
